package com.twu.biblioteca;

import com.twu.biblioteca.dao.BookResource;
import com.twu.biblioteca.dao.MovieResource;
import com.twu.biblioteca.dao.UserResource;
import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Entity;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.model.User;

import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private static BookResource bookResource;
    private static MovieResource movieResource;
    private static UserResource userResource;
    private User credential;

    public static void main(String[] args) {
        System.out.println("Hello, world!");
        bookResource = new BookResource("books.csv");
        movieResource = new MovieResource("movies.csv");
        userResource = new UserResource("users.csv");
        BibliotecaApp app = new BibliotecaApp();
        while(true) {
            app.showMenu();
            System.out.print("select menu: ");
            Scanner in = new Scanner(System.in);
            if (!in.hasNextInt()) {
                app.invalidMenuOption();
            } else {
                switch (in.nextInt()) {
                    case 1:
                        app.loadEntityDetail(bookResource.getAvailableList());
                        break;
                    case 2:
                        app.checkoutBook();
                        break;
                    case 3:
                        app.returnBook();
                        break;
                    case 4:
                        app.loadEntityDetail(movieResource.getAvailableList());
                        break;
                    case 5:
                        app.checkoutMovie();
                        break;
                    case 6:
                        app.quit();
                        break;
                    case 7:
                        app.showCheckoutBookHistory();
                        break;
                    case 8:
                        app.showUserProfile();
                        break;
                    default:
                        app.invalidMenuOption();
                        break;
                }
            }
        }

    }

    private void showUserProfile() {
        if (credential != null) {
            System.out.println(credential.getId() + ".  " + credential.getName() + "  " + credential.getEmail() + "  " + credential.getPhoneNumber() + "  " + credential.getLibraryNumber());
        } else {
            invalidMenuOption();
        }
    }

    private void showCheckoutBookHistory() {
        List<Entity> books = bookResource.getCheckoutList();
        if (books != null && books.size() > 0) {
            for (Entity entity : books) {
                Book book = (Book) entity;
                User user = userResource.getUserById(book.getBorrowId());
                System.out.println(book.getName() + ": " + user.getName());
            }
        } else {
            System.out.println("No one borrow book yet.");
        }
    }

    private void checkoutMovie() {
        if (checkCredential() != null) {
            System.out.print("Whick Movie you want to checkout? Movie: ");
            Scanner number = new Scanner(System.in);
            int targetPosition = number.nextInt();
            boolean checkoutSuccess =  movieResource.checkoutEntity(targetPosition, credential);
            if (checkoutSuccess) {
                System.out.println("Thank you! Enjoy the movie");
            } else {
                System.out.println("The movie is not available");
            }
        } else {
            System.out.println("Authenticate failed");
        }
    }

    public void checkoutBook() {
        if (checkCredential() != null) {
            System.out.print("Whick book you want to checkout? Book: ");
            Scanner number = new Scanner(System.in);
            int targetPosition = number.nextInt();
            boolean checkoutSuccess =  bookResource.checkoutEntity(targetPosition, credential);
            if (checkoutSuccess) {
                System.out.println("Thank you! Enjoy the book");
            } else {
                System.out.println("The book is not available");
            }
        } else {
            System.out.println("Authenticate failed");
        }
    }

    public void returnBook() {
        if (checkCredential() != null) {
            if (bookResource.getCheckoutList() == null || bookResource.getCheckoutList().size() == 0) {
                System.out.println("There is no book to return");
            } else {
                loadEntityDetail(bookResource.getCheckoutList());
                System.out.print("Choose the book you want to return: ");
                Scanner number = new Scanner(System.in);
                int targetPosition = number.nextInt();
                boolean returnSuccess =  bookResource.returnEntity(targetPosition);
                if (returnSuccess) {
                    System.out.println("Thank you for returning the book");
                } else {
                    System.out.println("That is not a valid book to return");
                }
            }
        } else {
            System.out.println("Authenticate failed");
        }
    }

    public void quit() {
        System.out.println("App quit successfully.");
        System.exit(0);
    }

    public void invalidMenuOption() {
        System.out.println("Select a valid option!");
    }

    public void showMenu() {
        String[] menus = {"List Books[1]", "Checkout Book[2]", "Return Book[3]", "ListMovies[4]", "Checkout Movie[5]", "Quit[6]", "CheckoutHistory[7]"};
        for (String menu : menus) {
            System.out.print(menu + "  ");
        }
        if (credential != null) {
            System.out.print("Profile[8]");
        }
        System.out.println();
    }

    public User checkCredential() {
        if (credential == null) {
            System.out.println("Credential required, please input your LibraryNumber and Password");
            System.out.print("LibraryNumber: ");
            Scanner in = new Scanner(System.in);
            String libraryNumber = in.nextLine();
            System.out.print("password: ");
            in = new Scanner(System.in);
            String password = in.nextLine();
            credential = userResource.authenticate(libraryNumber, password);
        }
        return credential;
    }

    public void loadEntityDetail(List<Entity> entities) {
        if (checkCredential() != null) {
            for (int i = 0; i < entities.size(); i++) {
                final Entity entity = entities.get(i);
                if (entity instanceof Book) {
                    Book book = (Book) entity;
                    System.out.println( book.getId() + ". " +book.getName() + ", " + book.getAuthor() + ", " + book.getYear());
                } else if (entity instanceof Movie) {
                    Movie movie = (Movie) entity;
                    System.out.println( movie.getId()+ ". " +movie.getName() + ", " + movie.getAuthor() + ", " + movie.getYear() + ", " + movie.getRating());
                }
            }
        } else {
            System.out.println("Authenticate failed");
        }
    }

}
